%% Filter stocks_id in same sector
clc
clear all

% Sector 1: Retail                % Sector 11: Chemistry
% Sector 2: Insurance             % Sector 12: Bank
% Sector 3: Real Estate           % Sector 13: Automobile
% Sector 4: IT                    % Sector 14: Natural Resources
% Sector 5: Oil & Gas             % Sector 15: FMCG
% Sector 6: Financial Services    % Sector 16: Media
% Sector 7: Electricity           % Sector 17: Telecom
% Sector 8: Hospitality           % Sector 18: Construction
% Sector 9: Heavy Industry        % Sector 19: Healthcare
% Sector 10: Household Products               

sector_id = 15;
all_sec_id_ok = get_sec_in_same_sector(sector_id);

sub_sec_id_ok = all_sec_id_ok(1:end);
% Select only 1 stock (327 MSN)
%sub_sec_id_ok = 327;

%%
matXX = [];
vecYY = [];

fromTime = '01/01/2007';
toTime = '31/03/2013';

forecastDay = 2;
%gap_ret = forecastDay+1;       

for i = 1:size(sub_sec_id_ok,1)
    rslt = get_info_from_database('security', 'sec_id', ...
        sub_sec_id_ok(i), 'from', fromTime, 'to', toTime);
    %rsult = [date, vol, open, high, low, close]
    if (~isempty(rslt))
        volume = rslt(:,2);
        hi = rslt(:,4);
        lo = rslt(:,5);
        clo = rslt(:,6);
        emaShort  = indicators(clo ,'ema',7);
        emaLong  = indicators(clo ,'ema',34);
        macdArr = indicators(clo ,'macd',12,26,9);
        adxArr = indicators([hi,lo,clo],'adx',14);
        cci = indicators([hi,lo,clo],'cci'    ,14,20,0.15);
        roc = indicators(clo        ,'roc'    ,15);
        rsi = indicators(clo        ,'rsi'    ,14);
        willr = indicators([hi,lo,clo],'william',14);
        matX = [volume clo emaShort emaLong macdArr adxArr cci roc rsi willr];

        %vec y, forecast 1 day
        vecRet = (clo(2:end,1)-clo(1:end-1,1))./clo(1:end-1,1);
        matX = matX(1:end-1,:);
        vecY = vecRet;
        
        %forecast >=2 day
        for jgap = 1:forecastDay-1
            vecY(1:end-jgap,1) = vecY(1:end-jgap,1) + vecRet(1+jgap:end,1);
        end
        vecY = vecY(1:end-(forecastDay-1));
        matX = matX(1:end-(forecastDay-1),:);
        %remove NaN data
        matX = matX(34:end,:);
        vecY = vecY(34:end,:);
        matXX = [matXX;matX];
        vecYY = [vecYY; vecY];
    end
end
inputs = matXX';
targets = vecYY';

hiddenLayerSize = 10;
net = fitnet(hiddenLayerSize);
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;

[net,tr] = train(net,inputs,targets);
outputs = net(inputs);
errors = gsubtract(targets,outputs);
performance = perform(net,targets,outputs)


